package com.dz.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dz.risk.common.SysConstant;
import com.dz.risk.common.exception.RRException;
import com.dz.risk.common.utils.DateUtils;
import com.dz.risk.dao.SystemLogDao;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.SystemLogEntity;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.SystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Slf4j
@Service("systemLogService")
public class SystemLogServiceImpl extends ServiceImpl<SystemLogDao, SystemLogEntity> implements SystemLogService {
    @Autowired
    private SystemLogDao systemLogDao;

    @Override
    public IPage<SystemLogEntity> querySystemLog(PageModel pageModel, SystemLogEntity systemLogEntity) throws RRException {
        IPage<SystemLogEntity> page = new Page<>();
        page.setCurrent(pageModel.getPageNum());
        page.setSize(pageModel.getPageSize());
        QueryWrapper<SystemLogEntity> wrapper = new QueryWrapper<SystemLogEntity>();
        wrapper.lambda()
                .like(!StringUtils.isEmpty(systemLogEntity.getManipulationMenu()),SystemLogEntity::getManipulationMenu, systemLogEntity.getManipulationMenu())
                .like(!StringUtils.isEmpty(systemLogEntity.getOperationContent()),SystemLogEntity::getOperationContent, systemLogEntity.getOperationContent())
                .eq(!StringUtils.isEmpty(systemLogEntity.getActionType()),SystemLogEntity::getActionType, systemLogEntity.getActionType())
                .eq(!StringUtils.isEmpty(systemLogEntity.getCreatedUser()),SystemLogEntity::getCreatedUser, systemLogEntity.getCreatedUser())
                .like(!StringUtils.isEmpty(systemLogEntity.getCreatedTime()),SystemLogEntity::getCreatedTime, DateUtils.dateToStringYears(systemLogEntity.getCreatedTime()))
                .orderByDesc(SystemLogEntity::getCreatedTime);
        IPage<SystemLogEntity> systemLogEntityIPage = systemLogDao.selectPage(page, wrapper);
        return systemLogEntityIPage;
    }

    /**
     * 获取时间
     *
     * @param time
     * @param days 需要添加或者减少的天数
     * @return
     * @throws ParseException
     */
    private String getTime(String time, int days) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar instance = Calendar.getInstance();
        instance.setTime(simpleDateFormat.parse(time));
        instance.add(Calendar.DATE, days);
        int mon = instance.get(Calendar.MONTH) + 1;
        String month = mon > 10 ? String.valueOf(mon) : "0" + mon;
        String day = instance.get(Calendar.DATE) > 10 ? String.valueOf(instance.get(Calendar.DATE)) : "0" + instance.get(Calendar.DATE);
        return instance.get(Calendar.YEAR) + "-" + month + "-" + day;
    }

    @Override
    public void addStSystemLog(SystemLogEntity systemLogEntity) {
        systemLogDao.insert(systemLogEntity);
    }


}